<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */
	defined ('IN_AGORA') or die;
ainclude('include|string');
	class TaskController extends AgoraController
	{
		function _default()
		{
			if (!$this->agora_config['o_rss_enabled']) {
				// empty feed
				$this->loadDefaultView();
				
				$this->view->assign('items',array());
				$this->view->assign('title',$this->agora_config['o_board_title']);
				$this->view->assign('link',Agora::makeFeedUrl(array()));
				$this->view->assign('description',$this->agora_config['o_board_desc']);
				return;
			}

			$tid = (int)Agora::getVar('tid');
			$fid = (int)Agora::getVar('fid');
			$cid = (int)Agora::getVar('cid');
			$key = Agora::getVar('key');

			$user_model = &Model::getInstance('UserModel');
			if (!$key) {
				$this->agora_user = $user_model->loadGuest();
			} else {
				$this->agora_user = $user_model->loadByKey($key);
				if (!$this->agora_user) {
					$this->agora_user = $user_model->loadGuest();
				}
			}

			$this->loadDefaultView();
			$this->model = & Model::getInstance('FeedModel');
			
			$this->topic_model = & Model::getInstance('TopicModel');

			if (! ($tid || $cid || $fid) ) {
				$this->_global();
				return;
			}

			if ($tid) {
				$this->authenticateTopic($tid,'read_rss');
				$this->_topic($tid);
				return;
			}

			if ($fid) {
				$this->authenticate($fid,'read_rss');
				$this->_forum($fid);
				return;
			}

			if ($cid) {
				$this->_category($cid);
				return;
			}
		}

		function _processPosts(& $posts, $topic_id = null) {
			$items = array();
			$all = array();
			
			jimport('joomla.utilities.date');
			$my = & JFactory::getUser();
			$timezone = $my->getParam('timezone',0);
			$timezone = (int)$timezone == 0 ? date('Z')/3600 : $timezone;
			
			$reverse = (int) $this->agora_user['reverse_posts'];
			$per_page = intval($this->agora_user['disp_posts']) > 0 ? $this->agora_user['disp_posts'] : intval($this->agora_config['o_disp_posts_default']);
			
			if ($topic_id) {
				$num_posts = $this->topic_model->getPostCount($topic_id);
			}
			
			$parser = & $this->helper('parser');
			//~ $parser = & $this->helper('parser');
			//~ $post_helper = & $this->helper('post');
			//~ $posts = $post_helper->processPosts($posts, $parser);
			
			if (is_array($posts) && count($posts) > 0) {
				foreach ($posts as $k => $post) {
					// determine the page of the post
					if ($topic_id) {				
						$num = $k;
					} else {
						if (!array_key_exists($post['topic_id'], $all)) {
							$all[$post['topic_id']] = $this->model->getTopicAll($post['topic_id']);
						}
						$num_posts = count($all[$post['topic_id']]);
						$num = array_search($post['id'], $all[$post['topic_id']]);
					}
					$current = (!$reverse) ? ($num_posts - $num) : ($num + 1);
					$page = ceil($current / $per_page);
					
					$url = array('task'=>'topic','id'=>$post['topic_id'], 'p' => $page);

					$date = new JDate(intval($post['posted']),$timezone);
					
					$post['message'] = $parser->parseMessage($post['message'], $post['hide_smilies']);
					
					$items[] = array (
							'title' => $post['topic_subject'],
							'date' => $date->toRFC822(),
							'description' => $post['message'],
							'link' => Agora::makeFeedUrl($url).'#p'.$post['id'],
						);
				}
			}
			return $items;
		}

		function _global()
		{
			$posts = $this->model->getGlobalFeed();

			$access_model = & Model::getInstance('AccessModel');
			$allowed_forums = $access_model->getAllowedForums($this->agora_user['id'],'read_rss');
			
			if (is_array($posts) && count($posts) > 0) {
				foreach ($posts as $key=>$post) {
					if (!in_array($post['forum_id'],$allowed_forums)) {
						unset($posts[$key]);
					}
				}
			}
			
			$this->view->assignRef('items',$this->_processPosts($posts));
			$this->view->assign('title',$this->agora_config['o_board_title']);
			$this->view->assign('link',Agora::makeFeedUrl(array()));
			$this->view->assign('description',$this->agora_config['o_board_desc']);
		}

		function _category($cat_id)
		{
			$posts = $this->model->getCategoryFeed($cat_id);

			$access_model = & Model::getInstance('AccessModel');
			$allowed_forums = $access_model->getAllowedForums($this->agora_user['id'],'read_rss');

			foreach ($posts as $key=>$post) {
				if (!in_array($post['forum_id'],$allowed_forums)) {
					unset($posts[$key]);
				}
			}

			$this->view->assignRef('items',$this->_processPosts($posts));
			$this->view->assign('title',$this->agora_config['o_board_title']);
			$this->view->assign('link',Agora::makeFeedUrl(array()));
			$this->view->assign('description',$this->agora_config['o_board_desc']);
		}


		function _forum($forum_id)
		{
			$posts = $this->model->getForumFeed($forum_id);

			$this->view->assignRef('items',$this->_processPosts($posts));
			$this->view->assign('title',$this->agora_config['o_board_title']);
			$this->view->assign('link',Agora::makeFeedUrl(array()));
			$this->view->assign('description',$this->agora_config['o_board_desc']);
		}

		function _topic($topic_id)
		{
			$posts = $this->model->getTopicFeed($topic_id);
			
			$this->view->assignRef('items',$this->_processPosts($posts, $topic_id));
			$this->view->assign('title',$this->agora_config['o_board_title']);
			$this->view->assign('link',Agora::makeFeedUrl(array()));
			$this->view->assign('description',$this->agora_config['o_board_desc']);
		}
	}
	
?>
